package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {
    @Autowired
    private EmpExprService empExprService;
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public Result getPageResult(EmpQueryParam param) {
        //设置分页参数
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());
        String name = param.getName();
        Integer gender = param.getGender();
        LocalDate begin = param.getBegin();
        LocalDate end = param.getEnd();
        LambdaQueryWrapper<Emp> lqw = new LambdaQueryWrapper<Emp>()
                //三个参数：1：条件是否成立true成立 false不成立  2：查询的列名  3：值
                //参数一为true时才拼接该条件
                .like(StringUtils.isNotBlank(param.getName()),Emp::getName, name)
                .eq(ObjectUtils.isNotNull(gender), Emp::getGender, gender)
                .between(ObjectUtils.isNotNull(begin) && ObjectUtils.isNotNull(end),Emp::getEntryDate,begin,end);
        this.page(page,lqw);
        List<Emp> emps = page.getRecords();
        Set<Integer> deptIds = emps.stream().map(Emp::getDeptId).collect(Collectors.toSet());
        List<Dept> depts = deptMapper.selectList(new LambdaQueryWrapper<Dept>().in(Dept::getId, deptIds));
        Map<Integer, String> deptMap = depts.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));
        emps.forEach(v->v.setDeptName(deptMap.get(v.getDeptId())));
        PageResult<Emp> pageResult = new PageResult<>(page.getTotal(), emps);
        return Result.success(pageResult);
    }

    @Override
    @Transactional
    public boolean saveEmpAndExprList(Emp emp) {
        this.save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(v -> v.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);
        return true;
    }

    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>()
                .eq(EmpExpr::getEmpId,emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        if(exprList != null && !exprList.isEmpty()){
            exprList.forEach(expr->expr.setEmpId(emp.getId()));
        }
        empExprService.saveBatch(exprList);
    }

    @Override
    public void removeEmpsByIds(List<Integer> ids) {
        removeBatchByIds(ids);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));
    }


}
